package com.dmall.sort.test;

import java.util.*;

/**
 * @author xiao1.wang
 * @date 2021/10/7 13:24
 */
public class Application2 {

    private static final Random RANDOM = new Random();

    public static List<Integer> generateRandomArray(int count, int rangeL, int rangeR) {
        assert count > 0 && rangeR >= rangeL;
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < count; i++) {
            int randomNum = RANDOM.nextInt(rangeR - rangeL) + rangeL;
            int element = randomNum - rangeL;
            list.add(element);
        }
        return list;
    }

    public static List<Integer> generateOrderedArray(int count, int rangeL, int rangeR) {
        List<Integer> list = generateRandomArray(count, rangeL, rangeR);
        Collections.sort(list);
        return list;
    }

    public static List<Integer> generateNearlyOrderedArray(int count, int rangeL, int rangeR, int swapTimes) {
        assert swapTimes >= 0;
        List<Integer> list = generateOrderedArray(count, rangeL, rangeR);
        for (int i = 0; i < swapTimes; i++) {
            int x = RANDOM.nextInt(list.size());
            int y = RANDOM.nextInt(list.size());
            swap(list, x, y);
        }
        return list;
    }

    private static void swap(List<Integer> list, int x, int y) {
        int temp = list.get(x);
        list.set(x, list.get(y));
        list.set(y, temp);
    }

    public static void main(String[] args) {
        /*List<Integer> list = generateRandomArray(101, 1, 200);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);*/
        List<Integer> list = generateNearlyOrderedArray(101, 1, 200, 2);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }
}
